package net.caixiaomi.info.adapter;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.model.LotteryItem;
import net.caixiaomi.info.util.Constants;

import java.util.List;

/**
 * 开奖列表
 */

public class LotteryAdapter extends BaseMultiItemQuickAdapter<LotteryItem, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public LotteryAdapter(List<LotteryItem> data) {
        super(data);
        addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_lottery);
        addItemType(Constants.ITEM_VIEW_HEADER, R.layout.header_football);
    }

    @Override
    protected void convert(BaseViewHolder helper, LotteryItem item) {
        try {
            switch (helper.getItemViewType()) {
                case Constants.ITEM_VIEW_HEADER:
                    bindHead(helper, item);
                    break;
                case Constants.ITEM_VIEW_ITEM:
                    bindItem(helper, item);
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void bindHead(BaseViewHolder helper, LotteryItem item) {
        helper.setGone(R.id.image, false);
        helper.setGone(R.id.btn_expand, false);
        String text = CommonApp.getInstance().getString(R.string.C_MATCH_RESULT_TOTAL_COUNT, item.getTotalCount(), item.getFinishCount());
        SpannableString spannableString = new SpannableString(text);
        spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), text.length() - 1 - String.valueOf(item.getFinishCount()).length(), text.length() - 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.title, spannableString);
    }

    private void bindItem(BaseViewHolder helper, LotteryItem item) {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(item.getChangci()).append(" ").append(item.getLeagueAddr()).append(" ").append(item.getMatchTime());

            helper.setText(R.id.text, stringBuilder);

            helper.setText(R.id.host, item.getHomeTeamAbbr());
            helper.setText(R.id.guest, item.getVisitingTeamAbbr());

            stringBuilder.setLength(0);
//            matchfinish ：1未结束 0已结束 2未开赛
            if (TextUtils.equals(item.getMatchFinish(), "2")) {
                stringBuilder.append(CommonApp.getInstance().getString(R.string.C_NO_START_MATCH));
            } else if (TextUtils.equals(item.getMatchFinish(), "0") || TextUtils.equals(item.getMatchFinish(), "1")) {
                if (!TextUtils.isEmpty(item.getFirstHalf())) {
                    stringBuilder.append(CommonApp.getInstance().getString(R.string.C_HALF_MATCH)).append(" ").append(item.getFirstHalf()).append("     ");
                }
                if (!TextUtils.isEmpty(item.getWhole())) {
                    stringBuilder.append(CommonApp.getInstance().getString(R.string.C_ALL_SCORE)).append(item.getWhole());
                }
            }
            String info = stringBuilder.toString().trim();

            helper.setGone(R.id.info, !TextUtils.isEmpty(info));
            helper.setText(R.id.info, info);
            RequestOptions options = new RequestOptions();
            options.error(R.mipmap.ic_football);
            options.placeholder(R.mipmap.ic_football);
            Glide.with(CommonApp.getInstance()).load(item.getHomeTeamLogo()).apply(options).into((ImageView) helper.getView(R.id.icon_host));
            Glide.with(CommonApp.getInstance()).load(item.getVisitingTeamLogo()).apply(options).into((ImageView) helper.getView(R.id.icon_guest));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
